package com.szholly.pro.watergas_server.ps.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.szholly.pro.watergas_server.common.data.DataUtil;
import com.szholly.pro.watergas_server.common.web.BaseController;
import com.szholly.pro.watergas_server.common.web.RequestUnit;
import com.szholly.pro.watergas_server.ps.service.StandardVerificationService;
import com.szholly.utils.spring.SpringBeanFactory;
import com.szholly.data.general.ITable;
import com.szholly.data.general.ITableFactory;
import com.szholly.data.general.PageInfo;
import com.szholly.data.general.RowBase;
import com.szholly.plug.form.excel.utils.StringUtil;

@Controller
@RequestMapping("/StandardVerification/*")
public class OperationStandardVerificationController extends BaseController { 
	protected Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	StandardVerificationService standardVerificationService;

	/**
	 * 从请求中获取参数
	 * 
	 * @return
	 */
	private Map<String, String> getParams() {
		Map<String, String> map = new HashMap<String, String>();
		String task_code = RequestUnit.getParameter("TASK_CODE");
		if (StringUtil.IsNotNullOrEmpty(task_code)) {
			map.put("task_code", task_code);
		}
		String start = RequestUnit.getParameter("START");
		if (StringUtil.IsNotNullOrEmpty(start)) {
			map.put("start", start);
		}
		String end = RequestUnit.getParameter("END");
		if (StringUtil.IsNotNullOrEmpty(end)) {
			map.put("end", end);
		}
		String wwtp_id = RequestUnit.getParameter("WWTP_ID");
		if (StringUtil.IsNotNullOrEmpty(wwtp_id)) {
			map.put("wwtp_id", wwtp_id);
		}
		String flow_type = RequestUnit.getParameter("FLOW_TYPE");
		if (StringUtil.IsNotNullOrEmpty(flow_type)) {
			map.put("flow_type", flow_type);
		}
		String station_id = RequestUnit.getParameter("STATION_ID");
		if (StringUtil.IsNotNullOrEmpty(station_id)) {
			map.put("station_id", station_id);
		}
		String water_type = RequestUnit.getParameter("WATER_TYPE");
		if (StringUtil.IsNotNullOrEmpty(water_type)) {
			map.put("water_type", water_type);
		}
		String station_name = RequestUnit.getParameter("STATION_NAME");
		if (StringUtil.IsNotNullOrEmpty(station_name)) {
			map.put("station_name", station_name);
		}
		return map;
	}

	/**
	 * 获取标样核查记录
	 * 
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value = "/getVeriList")
	public void getVeriList(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		PageInfo pageInfo = DataUtil.getPageInfo();
		ITableFactory tableFactory = (ITableFactory) SpringBeanFactory.getBean("TableFactory");
		ITable pTable = tableFactory.openTable("TASKQUERYDIC");
		List<RowBase> list = standardVerificationService.getStandardVerificationList(getParams(), pTable, pageInfo);
		String result = DataUtil.listToJSONObject(list,null,pageInfo).toString();
		outString(result);
	}

	/**
	 * 导出标样核查记录表
	 * 
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value = "/exportDoc")
	public void exportDoc(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		Map<String, String> params = getParams();
		Map<String, Object> root = new HashMap<String, Object>();
		ITableFactory tableFactory = (ITableFactory) SpringBeanFactory.getBean("TableFactory");
		ITable pTable = tableFactory.openTable("TASKQUERYDIC");
		List<RowBase> list = standardVerificationService.getStandardVerificationList(params, pTable,
				DataUtil.getPageInfo());
		root.put("list", list);
		RowBase row = null;
		if (list != null && list.size() > 0) {
			row = list.get(0);
			Date date = (Date) row.getValue("CREATE_TIME");
			root.put("DATE", date);
			root.put("USER", row.getSafeString("C_USEREALNAME"));
		}

		String OUNAME = "";
		if (params.containsKey("station_id")) {
			OUNAME = commonService.getOperationUnitByStaionId(params.get("station_id"));
		} else if (params.containsKey("wwtp_id")) {
			OUNAME = commonService.getOperationUnit(params.get("wwtp_id"));
		}
		root.put("OUNAME", OUNAME);

		if (params.containsKey("station_id")) {
			row = commonService.getStationGroupInfo(params.get("station_id"));
			if (row != null) {
				root.put("GROUP_LEADER", row.getSafeString("GROUP_LEADER"));
			}
			root.put("STATION_NAME", params.get("station_name"));
		} else if (params.containsKey("wwtp_id") && params.containsKey("flow_type")) {
			row = commonService.getStation(params.get("wwtp_id"), params.get("flow_type"));
			if (row != null) {
				String stationName = row.getSafeString("NAME");
				String stationId = row.getSafeString("CODE");
				root.put("STATION_NAME", stationName);
				row = commonService.getStationGroupInfo(stationId);
				if (row != null) {
					root.put("GROUP_LEADER", row.getSafeString("GROUP_LEADER"));
				}
			}
		}
		outWord(root, "standardSampleCheck.xml", "标样核查记录表.doc");
	}
	/**
	 * 导出标样核查记录表(地表水)
	 * 
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value = "/exportSWDoc")
	public void exportSWDoc(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		Map<String, String> params = getParams();
		Map<String, Object> root = new HashMap<String, Object>();
		ITableFactory tableFactory = (ITableFactory) SpringBeanFactory.getBean("TableFactory");
		ITable pTable = tableFactory.openTable("TASKQUERYDIC");
		List<RowBase> list = standardVerificationService.getStandardVerificationList(params, pTable,
				DataUtil.getPageInfo());
		List<RowBase> projectList = new ArrayList<RowBase>();
		List<RowBase> deviceList = standardVerificationService.getStandardVerifyDeviceList(params);
		root.put("list", list);
		root.put("deviceList", deviceList);
		HashSet<Object> set = new HashSet<>();
		String name=null;
		if(list.size()>0) {
			name = list.get(0).getSafeString("C_USEREALNAME");
			for(int i=0;i<list.size();i++) {
				RowBase rowBase = list.get(i);
				String testProject = rowBase.getSafeString("TEST_PROJECT_DESC");
				set.add(testProject);
			}
		}
		for(Iterator<Object> it = set.iterator();it.hasNext();) {
			RowBase row = new RowBase();
			row.setValue("TEST_PROJECT_DESC", it.next().toString());
			row.setValue("C_USEREALNAME", name);
			projectList.add(row);
		}
		
		root.put("list", list);
		root.put("PROJECT_LIST", projectList);
		RowBase row = null;
		if (list != null && list.size() > 0) {
			row = list.get(0);
			Date date = (Date) row.getValue("START_TIME");
			root.put("DATE", date);
			root.put("USER", row.getSafeString("C_USEREALNAME"));
			root.put("WEEK_NUMBER",row.getSafeDouble("WEEK_NUMBER"));
		}

		String OUNAME = "";
		if (params.containsKey("station_id")) {
			OUNAME = commonService.getOperationUnitByStaionId(params.get("station_id"));
		} else if (params.containsKey("wwtp_id")) {
			OUNAME = commonService.getOperationUnit(params.get("wwtp_id"));
		}
		root.put("OUNAME", OUNAME);

		if (params.containsKey("station_id")) {
			row = commonService.getStationGroupInfo(params.get("station_id"));
			if (row != null) {
				root.put("GROUP_LEADER", row.getSafeString("GROUP_LEADER"));
			}
			root.put("STATION_NAME", params.get("station_name"));
		} else if (params.containsKey("wwtp_id") && params.containsKey("flow_type")) {
			row = commonService.getStation(params.get("wwtp_id"), params.get("flow_type"));
			if (row != null) {
				String stationName = row.getSafeString("NAME");
				String stationId = row.getSafeString("CODE");
				root.put("STATION_NAME", stationName);
				row = commonService.getStationGroupInfo(stationId);
				if (row != null) {
					root.put("GROUP_LEADER", row.getSafeString("GROUP_LEADER"));
				}
			}
		}

		outWord(root, "sw/standardSampleCheck.xml", "标准液核查记录表.doc");

	}

	/**
	 * 获取字典项
	 * 
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value = "/getDataDomain")
	public void getJobList(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		String strDomainName = RequestUnit.getString("DomainName");
		String strSQL = " select * from DATA_DEFULATDOMAIN t where t.S_DOMAINNAME='" + strDomainName + "' ";
		String result = DataUtil.getPageSelectJson(strSQL, null);
		outString(result);
	}

}
